//
//  ICConversationListViewController.m
//  ICome
//
//  Created by zhangrongwu on 16/8/10.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICConversationListViewController.h"
#import "ICChatForwardCell.h"
#import "ICHeaderFile.h"
@interface ICConversationListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataList;
@property (nonatomic, strong)UISearchController *searchController;
@property (nonatomic, strong)UILabel *emptyStyleLabel;

@end

@implementation ICConversationListViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self setNav];
    [self getData];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f * Level;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICChatForwardCell *cell = [ICChatForwardCell cellWithTableView:tableView];
    cell.group = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ICGroup *group = [self.dataList objectAtIndex:indexPath.row];
    if (self.SelectGroupBlock) {
        self.SelectGroupBlock(group);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - CustomDelegate

#pragma mark - event response
// 按钮点击方法
#pragma mark - private methods
// 设置页面布局
- (void)setUI {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}
// 设置navigation
- (void)setNav {
    self.title = @"选择群组";
}
// 网络请求
- (void)getData {
    WEAKSELF;
    [ICMessageDatabase getGroups:^(NSArray *array, NSError *error) {
        weakSelf.dataList = (NSMutableArray *)array;
        [weakSelf.tableView reloadData];
    }];
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.userInteractionEnabled = YES;
        _tableView.backgroundColor = BACKGROUNDCOLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.tableHeaderView = self.searchController.searchBar;
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

//- (UISearchController *) searchController {
//    if (!_searchController) {
//        _searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchVC];
//        [_searchController setSearchResultsUpdater: self.searchVC];
//        [_searchController.searchBar setPlaceholder:@"搜索"];
//        [_searchController.searchBar setBarTintColor:BACKGROUNDCOLOR];
//        [_searchController.searchBar sizeToFit];
//        [_searchController.searchBar setDelegate:self];
//        [_searchController.searchBar.layer setBorderWidth:0.5f];
//        [_searchController.searchBar.layer setBorderColor:BACKGROUNDCOLOR.CGColor];
//        // 设置开始搜索时背景显示与否
//        _searchController.dimsBackgroundDuringPresentation = YES;
//        // 覆盖视图控制器的视图窗口的部分决定了呈现视图控制器的视图的大小。默认值是NO。
//        self.definesPresentationContext = YES;
//        _searchController.view.backgroundColor = [UIColor whiteColor];
//    }
//    return _searchController;
//}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

- (UILabel *)emptyStyleLabel {
    if (!_emptyStyleLabel) {
        _emptyStyleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, App_Frame_Width, 30)];
        _emptyStyleLabel.font = [UIFont systemFontOfSize:17];
        _emptyStyleLabel.backgroundColor = [UIColor clearColor];
        _emptyStyleLabel.textAlignment = NSTextAlignmentCenter;
        _emptyStyleLabel.textColor = ICRGB(0x9a9a9a);
        _emptyStyleLabel.text = @"您暂无团队或协作组织";
        [self.view addSubview:_emptyStyleLabel];
    }
    return _emptyStyleLabel;
}
@end
